package com.agent.platform.controller;

import com.agent.platform.common.PageResult;
import com.agent.platform.common.Result;
import com.agent.platform.dto.AgentHeartbeatDTO;
import com.agent.platform.dto.AgentRegisterDTO;
import com.agent.platform.service.AgentService;
import com.agent.platform.vo.AgentVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/agent")
@RequiredArgsConstructor
public class AgentController {
    
    private final AgentService agentService;
    
    @PostMapping("/register")
    public Result<Map<String, String>> register(@Valid @RequestBody AgentRegisterDTO dto) {
        return Result.success(agentService.register(dto));
    }
    
    @PostMapping("/unregister")
    public Result<Void> unregister(@RequestParam String agentId) {
        agentService.unregister(agentId);
        return Result.success();
    }
    
    @PostMapping("/heartbeat")
    public Result<Void> heartbeat(@Valid @RequestBody AgentHeartbeatDTO dto) {
        agentService.heartbeat(dto);
        return Result.success();
    }
    
    @GetMapping("/list")
    public Result<PageResult<AgentVO>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String agentType,
            @RequestParam(required = false) String status) {
        Page<AgentVO> page = agentService.list(current, size, agentType, status);
        PageResult<AgentVO> result = new PageResult<>(
            page.getTotal(), 
            page.getRecords(), 
            page.getCurrent(), 
            page.getSize()
        );
        return Result.success(result);
    }
    
    @GetMapping("/{agentId}")
    public Result<AgentVO> getDetail(@PathVariable String agentId) {
        return Result.success(agentService.getDetail(agentId));
    }
}
